#define _CRT_SECURE_NO_WARNINGS
#include<vector>
#include<queue>
using namespace std;
class Solution {
public:
    int dx[4] = { 1,-1,0,0 };
    int dy[4] = { 0,0,-1,1 };
    bool used[51][51];
    int row_size;
    int col_size;
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        row_size = grid.size();
        col_size = grid[0].size();

        int ret = 0;
        for (int i = 0; i < row_size; i++) {
            for (int j = 0; j < col_size; j++) {
                if (grid[i][j] == 1 && used[i][j] == false) {
                    ret = max(ret, BFS(grid, i, j));
                }
            }
        }
        return ret;
    }
    int BFS(vector<vector<int>>& grid, int i, int j) {
        queue<pair<int, int>> q;
        q.push({ i,j });
        used[i][j] = true;

        int size = 1;
        while (!q.empty()) {
            pair<int, int> index = q.front();
            q.pop();
            int x = index.first;
            int y = index.second;

            for (int i = 0; i < 4; i++) {
                int row = x + dx[i];
                int col = y + dy[i];
                if (row >= 0 && row < row_size && col >= 0 && col < col_size
                    && grid[row][col] == 1 && used[row][col] == false) {
                    q.push({ row,col });
                    used[row][col] = true;
                    size++;
                }
            }
        }
        return size;
    }
};